package com.etime.shycourse.controller.coupons;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.ListMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.dto.CouponrangePackageDto;
import com.etime.shycourse.pojo.Course;
import com.etime.shycourse.pojo.Coursepackage;
import com.etime.shycourse.services.coupons.interfaces.CoursepackageServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author wb
 * @since 2022-03-15
 */
@Api(tags = "后台-优惠券使用范围-课程包数据接口")
@RestController
@RequestMapping("/sys/coursepackage")
public class CoursepackageController {
    @Autowired
    private CoursepackageServices service;


    @ApiOperation("查询可以用课程")
    @GetMapping("/Course/list")
    public ListMessage<Course> selectCourseList() {
        List<Course> data = service.selectCourseList ();
        ListMessage<Course> msg = new ListMessage<> ();
        if (data.size () > 0) {
            msg.setCode (1);
            msg.setDatalist (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("课程数据");
        }
        return msg;
    }

    @ApiOperation("查询可以用课程包")
    @GetMapping("/list")
    public ListMessage<Coursepackage> selectCoursePackageList() {
        List<Coursepackage> data = service.selectCoursePackageList ();
        ListMessage<Coursepackage> msg = new ListMessage<> ();
        if (data.size () > 0) {
            msg.setCode (1);
            msg.setDatalist (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("课程数据");
        }
        return msg;
    }

    @ApiOperation("创建优惠券使用范围。返回int 成功 =1 失败=0 ")
    @PostMapping("/insert")
    public BaseMessage insertCouponRangePackage(@RequestBody CouponrangePackageDto packageDto) {
        BaseMessage msg = new BaseMessage ();
        int i = service.insertCouponRangePackage (packageDto);
        if (i > 0) {
            msg.setCode (i);
            msg.setMsg ("创建优惠券使用范围成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("创建优惠券使用范围失敗");
        }
        return msg;
    }

    @ApiOperation("创建课程包。返回int 成功 =1 失败=0 ")
    @PostMapping("/insert/package")
    public BaseMessage insertPackage(@RequestBody Coursepackage coursepackage) {
        BaseMessage msg = new BaseMessage ();
        int i = service.insertPackage (coursepackage);
        if (i > 0) {
            msg.setCode (i);
            msg.setMsg ("创建优惠券使用范围成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("创建优惠券使用范围失敗");
        }
        return msg;
    }

    @ApiOperation("修改课程包 ")
    @PostMapping("/upPackage")
    public BaseMessage updatePackageById(@RequestBody Coursepackage coursepackage) {
        BaseMessage msg = new BaseMessage ();
        int i = service.updatePackageById (coursepackage);
        if (i > 0) {
            msg.setCode (i);
            msg.setMsg ("修改课程包信息成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("修改课程包信息失敗");
        }
        return msg;
    }


    @ApiOperation("课程包分页")
    @GetMapping("/page")
    public PageMessage<Coursepackage> selectCoursePackagePage(Long pagenum, Long pagesize) {
        PageMessage<Coursepackage> msg = new PageMessage<> ();
        Page<Coursepackage> data = service.selectCoursePackagePage (pagenum, pagesize);
        if (data != null) {
            msg.setCode (1);
            msg.setPagedata (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有查询到数据");
        }
        return msg;
    }

    @ApiOperation("查询课程包")
    @GetMapping("/get/{packageId}")
    public PojoMessage<Coursepackage> selectCoursePackageById(@PathVariable Integer packageId) {
        PojoMessage<Coursepackage> msg = new PojoMessage<>();
        Coursepackage data = service.selectCoursePackageById(packageId);
        if (data != null) {
            msg.setCode(1);
            msg.setData(data);
        } else {
            msg.setCode(0);
            msg.setMsg("没有查询到课程包");
        }
        return msg;
    }

}

